package com.xinsoft.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.CusSafetyCheckParam;
import com.xinsoft.entity.po.CusSafetyCheck;
import com.xinsoft.service.CusSafetyCheckService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName CusSafetyCheckController
 * @Description 设备安监管理
 * @Author lijie
 * @Date 2023-6-13 12:53:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/cus/device/safetyCheck")
@Api(value = "CusSafetyCheckController", tags = {"设备安监管理控制层"})
public class CusSafetyCheckController {
	@Autowired
	private CusSafetyCheckService safetyCheckService;

	/**
	 * @Author Li9527
	 * @Description 设备安监管理-新增
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("新增")
	@PostMapping("/add")
	public Response<?> add(@RequestBody CusSafetyCheck safetyCheck) {
		// 参数校验
		ValidatorUtils.validateEntity(safetyCheck);
		CusSafetyCheck oldObj = safetyCheckService.getOne(new QueryWrapper<CusSafetyCheck>().eq("check_node",safetyCheck.getCheckNode()));
		if(oldObj != null){
			return Response.fail("该项检查事项已存在,请不要重复添加");
		}
		return Response.succeed(safetyCheckService.save(safetyCheck));
	}

	/**
	 * @Author Li9527
	 * @Description 设备安监管理-更新
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("更新")
	@PostMapping("/edit")
	public Response<?> edit(@RequestBody CusSafetyCheck safetyCheck) {
		// 参数校验
		ValidatorUtils.validateEntity(safetyCheck);
		CusSafetyCheck oldObj = safetyCheckService.getOne(new QueryWrapper<CusSafetyCheck>().eq("check_node",safetyCheck.getCheckNode()));
		if(oldObj != null && !oldObj.getId().equals(safetyCheck.getId())){
			return Response.fail("该项检查事项已存在,请不要重复添加");
		}
		return Response.succeed(safetyCheckService.updateById(safetyCheck));
	}


	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		safetyCheckService.removeByIds(ids);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 设备安监管理控分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/pageList")
	public Response<?> pageList(@Valid @RequestBody CusSafetyCheckParam param) {
//		if(param.getDeviceId() == null){
//			return Response.fail("设备id必传");
//		}
		return Response.succeed(safetyCheckService.findPageListByParam(param));
	}
}
